﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace CKS.Core.Framework
{
    public static partial class Ext
    {
        /// <summary>
        /// Framework 属性映射赋值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source"></param>
        /// <param name="destination"></param>
        public static void MapTo<T>(this T source, T destination)
        {
            if (source == null || destination == null)
            {
                return;
            }
            // 获取源对象和目标对象的类型
            Type type = typeof(T);
            // 获取类型的所有公共属性
            PropertyInfo[] properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);

            foreach (PropertyInfo property in properties)
            {
                // 检查属性是否可写
                if (property.CanWrite)
                {
                    // 获取源对象属性的值
                    object value = property.GetValue(source);
                    // 将值赋给目标对象的对应属性
                    property.SetValue(destination, value);
                }
            }
        }
    }
}
